﻿using BlockMain_Class;
using StackExchange.Redis;
using System;
using Newtonsoft.Json;

namespace BlcokMain_Record
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {

                //异步处理消息
                using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("127.0.0.1:6379"))
                {
                    ISubscriber sub = redis.GetSubscriber();

                    //订阅名为 messages 的通道

                    sub.Subscribe("peer", (channel, message) =>
                    {

                        //输出收到的消息
                        Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] {message}");
                        var data = message;//($"[{DateTime.Now:HH:mm:ss}] {message}");
                        var jsondata = Cache.Get(1 + data.ToString());
                        //保存记录
                        Records rec = new Records();
                        MongodbHelpers<Records> mon = new MongodbHelpers<Records>();
                        rec.JsonData = jsondata.ToString();
                        var result = mon.Insert(rec);

                    });
                    Console.WriteLine("正在监听队列...");
                    Console.ReadKey();
                }
            }
            catch (Exception e)
            {

                Console.WriteLine("出错信息:" + e.Message);
                Console.ReadKey();
            }
        }
    }
}
